﻿Imports System.Web.Mvc
Imports ADODBU
Namespace Controllers

    Public Class TrusteeController
        Inherits Web.Mvc.Controller
        <AcceptVerbs(HttpVerbs.Post)> _
        Public Function getTrustee()
            Dim mListJournal As New ADODBU.RowClassList(Of clsvi_GetTrusteeRow)
            Dim getUserId As New GetIdUser
            Dim strKdUser As String
            Dim vd As UserAccess = New UserAccess()

            RowClassUtil.RowClassFill(vd, RowClassUtil.GetASCIIStringFromBytes(Request.BinaryRead(Request.TotalBytes)))

            Dim tr As New clsvi_GetTrustee

            '-- cek user dan paswword
            If True Then
                '-- user dan password ok !!
                Dim cGroup As String = ""
                Dim agroup As New List(Of Crep)
                Dim bb As Crep
                strKdUser = getUserId.getKodeUser(vd.UserID)
                mListJournal = tr.GetRowList(Connection.GetDBConn, " where kd_user='" & strKdUser & "' and Mod_type=1 and (APP_ID)='NA' AND Aktif = 1 order by Mod_Row,Mod_Group,Mod_ID")
                For Each x As clsvi_GetTrusteeRow In mListJournal
                    x.Mod_URL = x.Mod_URL.Replace("[mod_id]", x.Mod_ID)
                    x.Mod_URL = x.Mod_URL.Replace("[image]", x.Mod_ImgURL)
                    x.Mod_URL = x.Mod_URL.Replace("[Mod_Name]", x.Mod_Name)
                    If Not cGroup.Contains(x.Mod_Group) Then
                        Dim mtmp As New ADODBU.RowClassList(Of clsvi_GetTrusteeRow)
                        Dim cHtm As String = ""
                        mtmp = tr.GetRowList(Connection.GetDBConn, " where kd_user='" & strKdUser & "' and (APP_ID)='SIASEF' and mod_type=2 and parent_group='" & x.Mod_Group & "' AND Aktif = 1 order by Mod_Row,Mod_Group,Mod_ID")
                        For Each g As clsvi_GetTrusteeRow In mtmp
                            g.Mod_URL = g.Mod_URL.Replace("[mod_id]", g.Mod_ID)
                            g.Mod_URL = g.Mod_URL.Replace("[image]", g.Mod_ImgURL)
                            g.Mod_URL = g.Mod_URL.Replace("[Mod_Name]", g.Mod_Name)
                            g.Mod_URL = g.Mod_URL.Replace("[Mod_Desc]", g.Mod_Desc)
                            cHtm += g.Mod_URL & vbCrLf
                        Next
                        bb = New Crep With {.Mod_Group = x.Mod_Group, .Htm = cHtm}
                        agroup.Add(bb)
                        cGroup += x.Mod_Group
                    End If
                Next

                Dim result = New With {.success = True, .total = mListJournal.Count, .ListTrustee = mListJournal, .ListReport = agroup}
                Return Json(result)
            Else
                Dim result = New With {.success = False, .total = 0, .ListTrustee = Nothing}
                Return Json(result)
            End If

        End Function
        Private Class GetIdUser
            Public Function getKodeUser(ByVal strUser As String) As String
                Dim strSQL As String
                Dim rec As New ADODBU.Recordset
                strSQL = "SELECT     dbo.aspnet_Users.UserName, dbo.ZUsers.Kd_User"
                strSQL += " FROM         dbo.aspnet_Users INNER JOIN"
                strSQL += "       dbo.ZUsers ON dbo.aspnet_Users.UserId = dbo.ZUsers.UserId"
                strSQL += " WHERE dbo.aspnet_Users.UserName='" & strUser & "'"
                rec.Open(strSQL, "aa", Connection.GetDBConn)
                If rec.RecordCount > 0 Then
                    getKodeUser = rec.Fields("Kd_User")
                Else
                    getKodeUser = ""
                End If
            End Function
        End Class
        Private Class GetUnitKerja
            Public Function getUnitKerja(ByVal strUser As String) As String
                Dim strSQL As String
                Dim rec As New ADODBU.Recordset
                Dim strList As String
                strSQL = "SELECT     * "
                strSQL += " FROM ZUsers_UnitKerja "
                strSQL += " WHERE Kd_User='" & strUser & "'"
                rec.Open(strSQL, "aa", Connection.GetDBConn)
                If rec.RecordCount > 0 Then
                    strList = "("
                    While Not rec.Eof
                        strList += "'" & rec.Fields("Kd_Unit_Kerja") & "',"
                        rec.MoveNext()
                    End While
                    strList = Left(strList, Len(strList) - 1)
                    strList += ")"
                Else
                    strList = "('')"
                End If

                getUnitKerja = strList
                Return strList
            End Function
        End Class
        Private Class Crep
            Dim _a As String
            Public Property Mod_Group() As String
                Get
                    Return _a
                End Get
                Set(ByVal value As String)
                    _a = value
                End Set
            End Property
            Dim _b As String
            Public Property Htm() As String
                Get
                    Return _b
                End Get
                Set(ByVal value As String)
                    _b = value
                End Set
            End Property

        End Class


        Public Function ViewUser() As ActionResult
            Return View()
        End Function


        Public Function ViewModules(ByVal ctargetMdl As String) As ActionResult
          
            Dim txt As String = ""
            txt = Request.Url.Query.Replace("?", "")
          
            Return View(New Models.ModuleExec(txt).ListJS)
        End Function
        Public Class getListKodeUnitKerja
            Public Shared GetStrListKodeUnitKerja As String = ""
        End Class



    End Class

End Namespace
